class Trie {
public:
    /** Initialize your data structure here. */
    Trie() 
    :children(26),isEnd(false)
    {}
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        Trie*cur=this;
        for(auto ch:word)
        {
            if(cur->children[ch-'a']==nullptr)//没有记录该字符，进行创建记录
            {
                cur->children[ch-'a']=new Trie();//创建结点
            }
            cur=cur->children[ch-'a'];//有结点移动到该结点，进行判断下一个字符
        }
        //最终判断完最后一个字符,当前处在该单词串的最后结点上
        cur->isEnd=true;
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        Trie* cur=this;
        for(auto ch:word)
        {
            if(cur->children[ch-'a']==nullptr)//未在树中找到该单词，单词不匹配
            {
                return false;
            }
            cur=cur->children[ch-'a'];
        }
        //走完表示树中有这些结点，但还需判断是否是某个单词的尾部即完整单词
        return cur&&cur->isEnd;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        Trie* cur=this;
        for(auto ch:prefix)
        {
            if(cur->children[ch-'a']==nullptr)//未在树中找到该单词，单词不匹配
            {
                return false;
            }
            cur=cur->children[ch-'a'];
        }
        //走完，前缀匹配
        return true;
    }
private:
    vector<Trie*>children;//26个字母，数组化哈希
    bool isEnd;//判断是否为完整单词
};

/**
 * Your Trie object will be instantiated and called as such:
 * Trie* obj = new Trie();
 * obj->insert(word);
 * bool param_2 = obj->search(word);
 * bool param_3 = obj->startsWith(prefix);
 */